﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;

namespace Models
{
    public class ItemMaxId
    {
        /// <summary>
        /// 最大的所有库里物品编号的最大ID
        /// </summary>
        /// <returns></returns>
        private static int ids(int type)
        {
            int id = 0;
            try
            {

                string sql = string.Empty;
                switch (type)
                {
                    case 1:
                        //仓库最大编号
                        sql = "select id from character_warehouse where id < 200000000 order by id desc Limit 1";
                        break;
                    case 2:
                        //背包最大编号
                        sql = "select id from character_items where id < 200000000 order by id desc Limit 1";
                        break;
                    case 3:
                        //妖仓最大编号
                        sql = "select id from character_elf_warehouse where id < 200000000 order by id desc Limit 1";
                        break;
                    case 4:
                        //盟仓最大编号
                        sql = "select id from clan_warehouse where id < 200000000 order by id desc Limit 1";
                        break;
                }
                MySqlCommand com = new MySqlCommand(sql, DBHelper.con);
                DBHelper.con.Open();
                id = Convert.ToInt32(com.ExecuteScalar());
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
            finally
            {
                DBHelper.con.Close();
                DBHelper.con.Dispose();
            }
            return id;
        }

        /// <summary>
        /// 最大的ID
        /// </summary>
        /// <returns></returns>
        private static int MaxID()
        {
            int maxID = 0;
            int a = ids(1);
            int b = ids(2);
            int c = ids(3);
            int d = ids(4);
            int[] intMax = { a, b, c, d };
            foreach (int tempint in intMax)
            {
                if (maxID < tempint)
                {
                    maxID = tempint;
                }
            }
            return maxID;
        }

        public static int ID()
        {
            int maxID = MaxID();
            if (maxID == 0)
            {
                return 99999999;
            }
            return maxID;
        }
    }
}
